// pages/index/listMsg/listMsg.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userType: 2,
    info: {},
    companyNavIndex: '0',
    companyList: [],
    showCompanyInfo: {},
    id: null,
    userId: '',
    isShowMore: false,
    bigImg: '',
  },
  showBigImg: function(e) {
    console.log('a')
    this.setData({
      bigImg: e.currentTarget.dataset.src
    })
  },
  hideBigimg: function() {
    this.setData({
      bigImg: ''
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      id: options.id,
      userId: app.globalData.userInfo.user_id,
      userType: options.type
    })
    this.getUserInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  // 分享
  onShareAppMessage(res) {
    return {
      title: '广州大学校友会校友企业家专委会',
      path: '/pages/index/index'
    }
  },
  // 获取用户信息
  getUserInfo: function() {
    let _this = this;
    app.util.ajax({
      url: "index/business",
      data: {
        user_id: app.globalData.userInfo.user_id,
        member_id: _this.data.id,
      },
      success: function(res) {
        let list = [];
        if (res.info.sub) {
          for (var i = 0, len = res.info.sub.length; i < len; i++) {
            let obj = res.info.sub[i];
            obj.navIndex = i;
            list.push(obj);
          }
        }
        // 屏蔽数据
        // if (res.info.state || (_this.data.id === app.globalData.userInfo.user_id)) {
        // } else {
        //   var reg = /^(\d{3})\d{4}(\d{4})$/
        //   res.info.mobile = res.info.mobile.replace(reg, '$1****$2');
        //   res.info.wechat = res.info.wechat.replace(reg, '$1****$2');
        // };
        _this.setData({
          info: res.info,
          companyList: list,
        })
        _this.changeCompanyInfo();
      }
    })
  },
  // 更换公司
  changeCompanyNav: function(e) {
    if (isNaN(e.target.dataset.index)) {
      return
    }
    let index = e.target.dataset.index;
    this.setData({
      companyNavIndex: index
    });
    this.changeCompanyInfo();
  },
  // 更新公司信息
  changeCompanyInfo: function() {
    let list = this.data.companyList.length > 0 ? this.data.companyList[Number(this.data.companyNavIndex)] : {};
    this.setData({
      showCompanyInfo: list
    })
  },
  // 更换显示更多
  changeShowMore: function() {
    this.setData({
      isShowMore: !this.data.isShowMore
    })
  },
  // 更换关注
  changeFollow: function(e) {
    let state = e.currentTarget.dataset.state,
      _this = this;
    app.util.ajax({
      url: 'index/follow',
      data: {
        user_id: app.globalData.userInfo.user_id,
        member_id: _this.data.id,
        state: state,
        com_state: _this.data.userType
      },
      success: function(res) {
        _this.getUserInfo();
      }
    })
  },
  warn() {
    wx.showModal({
      title: '提示',
      content: '请耐心等待审核',
      showCancel: false
    })
  },
  newDetails(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/news/newsDetails/newsDetails?id=' + id
    })
  }
})